// 厦门大学计算机专业 | 前华为工程师
// 专注《零基础学编程系列》https://cxyxy.blog.csdn.net/article/details/121134634
// 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
// 公众号：蓝不蓝编程

import SwiftUI

struct HspView: View {
    
    @ObservedObject var stopWatch = Stopwatch()
    
    var body: some View {
        VStack {
            Text(String(format: "%.1f", stopWatch.seconds))
                .font(.custom("Avenir", size: 80))
                .fontWeight(.semibold)
                .padding(.top, 50)
                .padding(.bottom, 50)
            HStack{
                if stopWatch.mode == .stopped {
                    Button(action: {self.stopWatch.start()}) {
                        TimerButton(label: "开始", buttonColor: .green)
                    }
                }
                if stopWatch.mode == .running {
                    Button(action: {self.stopWatch.pause()}) {
                        TimerButton(label: "暂停", buttonColor: .blue)
                    }
                    Button(action: {self.stopWatch.stop()}) {
                        TimerButton(label: "停止", buttonColor: .red)
                    }
                }
                if stopWatch.mode == .paused {
                    Button(action: {self.stopWatch.start()}) {
                        TimerButton(label: "开始", buttonColor: .blue)
                    }
                    Button(action: {self.stopWatch.stop()}) {
                        TimerButton(label: "停止", buttonColor: .red)
                    }
                }
            }
        }
    }
}

struct TimerButton: View {
    
    let label: String
    let buttonColor: Color
    
    var body: some View {
        Text(label)
            .font(.custom("Avenir", size: 20))
            .foregroundColor(.white)
            .frame(width: 50, height: 50, alignment: .center)
            .background(buttonColor)
            .clipShape(Circle())
            .padding(3)
            .overlay(
            RoundedRectangle(cornerRadius: 100)
                .stroke(buttonColor, lineWidth: 2))
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        HspView()
    }
}
